#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include "stdlib.h"

#define MAXSIZE 100
typedef int STDataType;
typedef int bool;
typedef struct Stack{
    STDataType* data;
    int top;  //栈顶
    int capacity;  //容量计数器
};
typedef struct Stack Stack;
//初始化
void StackInit(Stack* ps) {
    assert(ps); // 判断指针的合法性(断言 )
    ps->data = (STDataType*)malloc(sizeof (STDataType)*4);
    ps->top = 0;
    ps->capacity = 4;
    return;
}
//销毁
void StackDestroy(Stack* ps) {
    assert(ps);//d
    free(ps->data);
    ps->data = NULL;
    ps->capacity = 0;
    ps->top = 0;
}
//判断栈空
bool StackEmpty(Stack* ps) {
    assert(ps);
    return ps->top == 0;
}
//入栈
void StackPush(Stack* ps,STDataType x){
    assert(ps);
    //是否需要扩容
    if(ps->top == ps->capacity) {
        STDataType*  tmp = (Stack*) realloc(ps->data,sizeof (STDataType)*ps->capacity*2);//两倍扩容
        if(tmp == NULL) {
            printf("扩容失败");
            exit(-1);
        }
        ps->data = tmp;
        ps->capacity = ps->capacity*2;
    }
    //压入数据
    ps->data[ps->top] = x;
    ps->top++;
}
//出栈
void StackPop(Stack* ps) {
    assert(ps);
    assert(!StackEmpty(ps));
    ps->top--;
}
//获取栈顶元素
STDataType StackPeek(Stack* ps) {
    assert(ps);
    assert(!StackEmpty(ps));
    return ps->data[ps->top - 1];
}

void ConvertDec2Radix(Stack *ps, int dec, int radix) {
    do {
        StackPush(ps,dec%radix);
        dec = dec / radix;
    } while ((dec != 0));
}
int main() {
    Stack s1;
    StackInit(&s1);
    StackPush(&s1,2);StackPush(&s1,4);StackPush(&s1,3);StackPush(&s1,1);StackPush(&s1,7);
//
//    int s = StackPeek(&s1);
//    printf("%d",s1.data[s1.top]);

//    int a = 20;
//    int *pa = &a;
//    int **ppa = &pa;
//    printf("%p %p %p",a,*pa,**ppa);
//    int* arr[5];
//    printf("%p %p", arr ,*arr+1);
//    char ch = '2';
//    char *p = &ch;
//    char str[] = "hello";
//    printf("%c ",*(str+1) );
    return 0;
}
